//
//  CSLimitedController.m
//  CreateWithSuper
//
//  Created by chenjunbo on 2024/7/2.
//  Copyright © 2024 CS. All rights reserved.
//

#import "CSLimitedController.h"
#import "CSNetworkHelper.h"
#import "CSCreatingVC.h"
#import "SVProgressHUD.h"
#import "ADModule.h"

@interface CSLimitedController ()<UITextViewDelegate>

@property (weak, nonatomic) IBOutlet UITextView *textView;
@property (weak, nonatomic) IBOutlet UILabel *textViewCountLabel;
@property (weak, nonatomic) IBOutlet UILabel *textViewPLabel;

@property (nonatomic, strong) NSArray *defaultLuckArr;

@property (nonatomic, strong) NSString *lastRandomStr;
@property (weak, nonatomic) IBOutlet UIButton *b1;
@property (weak, nonatomic) IBOutlet UIButton *b2;
@property (weak, nonatomic) IBOutlet UIButton *b3;
@property (weak, nonatomic) IBOutlet UIButton *b4;
@property (weak, nonatomic) IBOutlet UIButton *b5;

@end

@implementation CSLimitedController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSInteger wordshow = [ADModule standardInstance].wordshow;
    if (wordshow == 1){
        self.b1.hidden = NO;
        self.b2.hidden = NO;
        self.b3.hidden = NO;
        self.b4.hidden = NO;
//        self.b5.hidden = NO;
    }else{
        self.b1.hidden = YES;
        self.b2.hidden = YES;
        self.b3.hidden = YES;
        self.b4.hidden = YES;
//        self.b5.hidden = YES;
    }
}

- (IBAction)b:(UIButton *)sender {
    self.textView.text = sender.titleLabel.text;
    [self textViewDidChange:self.textView];
}

- (IBAction)backClick:(UIButton *)sender {
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (IBAction)tryClick:(UIButton *)sender {
    
    NSString *randomStr = [self randomStr];
        
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Create" bundle:nil];
    CSCreatingVC *vc = [storyboard instantiateViewControllerWithIdentifier:@"CSCreatingVC"];
    vc.taskID = @"";
    vc.createStr = randomStr;
    vc.isLocal = YES;
    [self.navigationController pushViewController:vc animated:YES];

}

- (IBAction)createClick:(UIButton *)sender {
    
    if (self.textView.text.length == 0){
        [SVProgressHUD showErrorWithStatus:@"Please enter a description"];
        dispatch_time_t delay = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC));
        dispatch_after(delay, dispatch_get_main_queue(), ^{
            [SVProgressHUD dismiss];
        });
        return;
    }
    
    [self.view endEditing:YES];
    
    [[CSNetworkHelper standardInstance]requestTxt2img:self.textView.text callback:^(NSString * _Nonnull result) {
        
        if (result.length > 0){

            [[ADModule standardInstance]showInterAdWithVC:self dismiss:^{
                
            }];
            
            UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Create" bundle:nil];
            CSCreatingVC *vc = [storyboard instantiateViewControllerWithIdentifier:@"CSCreatingVC"];
            vc.taskID = result;
            vc.createStr = self.textView.text;
            vc.isLimited = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }
        
    }];
}

- (IBAction)clearallClick:(UIButton *)sender {
    self.textView.text = @"";
    self.textViewCountLabel.text = @"0/102";
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.textView endEditing:YES];
}

- (NSString *)randomStr {

    NSString *randomStr = self.defaultLuckArr[arc4random_uniform(self.defaultLuckArr.count)];
    
    if ([randomStr isEqualToString:self.lastRandomStr]){
        return [self randomStr];
    } else {
        self.lastRandomStr = randomStr;
        return randomStr;
    }
}

- (void)textViewDidChange:(UITextView *)textView {
    
    self.textViewCountLabel.text = [NSString stringWithFormat:@"%lu/102",(unsigned long)textView.text.length];
    
    if (textView.text.length > 0){
        self.textViewPLabel.hidden = YES;
    }else{
        self.textViewPLabel.hidden = NO;
    }
    
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    NSUInteger newTextLength = [textView.text length] + [text length] - range.length;
    if (newTextLength > 102) {
        return NO;
    }
    if ([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];
        return NO;
    }
    return YES;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
